package com.lj.back.manage.scaffold.common.convert;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.lj.back.manage.scaffold.common.response.Response;
import com.lj.back.manage.scaffold.common.util.EnumUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * @author lv
 * @version 1.0.0
 */
public class EnumConvertJsonSerializer extends JsonSerializer<String> {

    private Class<? extends Enum<? extends Response>> aClass;

    public EnumConvertJsonSerializer(Class<? extends Enum<? extends Response>> aClass){
        this.aClass = aClass;
    }

    @Override
    public void serialize(String code, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        String output = StringUtils.EMPTY;
        if (StringUtils.isNotBlank(code)) {
            Enum<? extends Response> anEnum = EnumUtil.getEnumByCode(aClass, code);
            output = ((Response)anEnum).getMessage();
        }
        jsonGenerator.writeString(output);
    }
}
